<?php
/**
 * Created by PhpStorm.
 * User: zhangrxiang
 * Date: 2017/6/23
 * Time: 14:39
 * Project name: PHPTEST
 * File name: 1.php
 * @copyright Copyright (c) 2016 ZRX Software LLC
 * @license https://github.com/zhangrxiang
 * @since 1.0.0
 */

//preg_match — 执行匹配正则表达式

//pattern
//要搜索的模式，字符串类型。
//
//subject
//输入字符串。
//
//matches
//如果提供了参数matches，它将被填充为搜索结果。 $matches[0]将包含完整模式匹配到的文本， $matches[1] 将包含第一个捕获子组匹配到的文本，以此类推。
//
//flags
//flags可以被设置为以下标记值：
//
//PREG_OFFSET_CAPTURE
//如果传递了这个标记，对于每一个出现的匹配返回时会附加字符串偏移量(相对于目标字符串的)。 注意：这会改变填充到matches参数的数组，使其每个元素成为一个由 第0个元素是匹配到的字符串，第1个元素是该匹配字符串 在目标字符串subject中的偏移量。


preg_match( '/(foo)(bar)(baz)/', 'foobarbaz', $matches, PREG_OFFSET_CAPTURE );
print_r( $matches );


$subject = "abcdef";
$pattern = '/^def/';
$pattern2 = '/def$/';
preg_match($pattern, $subject, $matches2, PREG_OFFSET_CAPTURE, 3);
print_r($matches2);
preg_match($pattern2, $subject, $matches3, PREG_OFFSET_CAPTURE, 3);
print_r($matches3);


//$init = curl_init("wp.dev");
//$html = curl_exec($init);
//preg_match( '/<title>(.*)<\/title>/siU', $html, $title_matches );
//
//print_r($title_matches);
//print_r($title_matches[0]);


//Example #1 查找文本字符串"php"
//模式分隔符后的"i"标记这是一个大小写不敏感的搜索
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
	echo "A match was found.";
} else {
	echo "A match was not found.";
}

preg_match("/php/i", "PHP is the web scripting language of choice.",$m);
print_r($m);


//Example #2 查找单词"word"
/* 模式中的\b标记一个单词边界，所以只有独立的单词"web"会被匹配，而不会匹配
 * 单词的部分内容比如"webbing" 或 "cobweb" */
if (preg_match('/\bweb\b/i', "PHP is the web scripting language of choice.",$m2)) {
	echo "A match was found.";
	print_r($m2);
} else {
	echo "A match was not found.";
}

if (preg_match('/\bweb\b/i', "PHP is the website scripting language of choice.",$m3)) {
	echo "A match was found.";
} else {
	echo "A match was not found.";
	print_r($m3);
}

echo str_repeat("*",50).PHP_EOL;
//从URL中获取主机名称
preg_match('@^(?:http://)?([^/]+)@i',
	"http://www.php.net/index.html", $matches);
$host = $matches[1];
echo $host.PHP_EOL;
//获取主机名称的后面两部分
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}\n";


$str = 'foobar: 2008';
preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $m4);
/* 下面例子在php 5.2.2(pcre 7.0)或更新版本下工作, 然而, 为了后向兼容, 上面的方式是推荐写法. */
// preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);
print_r($m4);



//	Simple regex
//
//	Regex quick reference
//	[abc]     A single character: a, b or c
//	[^abc]     Any single character but a, b, or c
//	[a-z]     Any single character in the range a-z
//	[a-zA-Z]     Any single character in the range a-z or A-Z
//	 ^     Start of line
//	 $     End of line
//	\A     Start of string
//	\z     End of string
//	 .     Any single character
//	\s     Any whitespace character
//	\S     Any non-whitespace character
//	\d     Any digit
//	\D     Any non-digit
//	\w     Any word character (letter, number, underscore)
//	\W     Any non-word character
//	\b     Any word boundary character
//	(...)     Capture everything enclosed
//	(a|b)     a or b
//	a?     Zero or one of a
//	a*     Zero or more of a
//	a+     One or more of a
//	a{3}     Exactly 3 of a
//	a{3,}     3 or more of a
//	a{3,6}     Between 3 and 6 of a
//
//	options: i case insensitive m make dot match newlines x ignore whitespace in regex o perform #{...} substitutions only once